package com.huizhonglm.diap.ui.user;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.huizhonglm.diap.R;
import com.huizhonglm.diap.api.model.user.MyWalletResponse;
import com.huizhonglm.diap.presenters.user.MyWalletPresenter;
import com.huizhonglm.diap.ui.BaseActivity;
import com.huizhonglm.diap.ui.user.iView.MyWalletIView;
import com.huizhonglm.diap.ui.wallet.BankCardListActivty;
import com.huizhonglm.diap.ui.wallet.IntegralCashBackActivity;
import com.huizhonglm.diap.ui.wallet.MyCashActivity;
import com.huizhonglm.diap.ui.wallet.MyPointActivity;
import com.huizhonglm.diap.ui.wallet.QueryQueueActivity;
import com.huizhonglm.diap.ui.wallet.RcmRevenueActivity;
import com.huizhonglm.diap.ui.wallet.RechargeActivity;
import com.huizhonglm.diap.ui.wallet.TPWDForgetActivity;
import com.huizhonglm.diap.ui.wallet.TPWDModifyActivity;
import com.huizhonglm.diap.ui.wallet.WithdrawActivity;
import com.huizhonglm.diap.widget.LoadView;

/**
 * 我的钱包
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2016-03-22 13:42
 */
public class MyWalletActivity extends BaseActivity implements MyWalletIView, View.OnClickListener,
        LoadView.OnLoadAgainListener {

    private double d_balance = 0d;
    private TextView balanceTxt;
    private TextView valBalanceTxt;
    private TextView integralTxt;
    private TextView cardCountTxt;
    private LoadView loadView;

    private MyWalletResponse myWallet;

    private MyWalletPresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_wallet);
        setBackEvent(this, null);
        mPresenter = new MyWalletPresenter(this);
        assignViews();
    }

    @Override
    protected void assignViews() {
        balanceTxt = (TextView) findViewById(R.id.balance_txt);
        valBalanceTxt = (TextView) findViewById(R.id.val_balance_txt);
        integralTxt = (TextView) findViewById(R.id.integral_txt);
        cardCountTxt = (TextView) findViewById(R.id.card_count_txt);
        View dataView = findViewById(R.id.my_wallet_scrl);
        loadView = (LoadView) findViewById(R.id.load_view);
        View bankCard = findViewById(R.id.bankcard_layout);
        View recharge = findViewById(R.id.recharge_txt);
        View withdraw = findViewById(R.id.withdraw_txt);
        View queryQueue = findViewById(R.id.query_queue_txt);
        View integralCash = findViewById(R.id.integral_cash_txt);
        View rcmRevenue = findViewById(R.id.rcm_revenue_txt);
        View tpwdForget = findViewById(R.id.tpwd_modify_txt);
        View tpwdModify = findViewById(R.id.tpwd_forget_txt);
        integralCash.setVisibility(View.GONE);
        queryQueue.setVisibility(View.GONE);

        loadView.setDataView(dataView, this);

        balanceTxt.setOnClickListener(this);
        integralTxt.setOnClickListener(this);
        bankCard.setOnClickListener(this);
        recharge.setOnClickListener(this);
        withdraw.setOnClickListener(this);
        queryQueue.setOnClickListener(this);
        integralCash.setOnClickListener(this);
        rcmRevenue.setOnClickListener(this);
        tpwdForget.setOnClickListener(this);
        tpwdModify.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        onLoadAgain();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.balance_txt:
                startAction(MyCashActivity.class);
                break;
            case R.id.integral_txt:
                startAction(MyPointActivity.class);
                break;
            case R.id.bankcard_layout:
                startAction(BankCardListActivty.class);
                break;
            case R.id.recharge_txt:
                startAction(RechargeActivity.class);
                break;
            case R.id.query_queue_txt:
                startAction(QueryQueueActivity.class);
                break;
            case R.id.integral_cash_txt:
                startAction(IntegralCashBackActivity.class);
                break;
            case R.id.rcm_revenue_txt:
                startAction(RcmRevenueActivity.class);
                break;
            case R.id.withdraw_txt:
//                if (myWallet != null && myWallet.getBalance() > 0) {
//                    Intent withdraw = new Intent(this, WithdrawActivity.class);
//                    withdraw.putExtra("balance", myWallet.getBalance());
//                    startActivity(withdraw);
//                } else {
//                    ToastUtil.makeTextShort("您的当前余额为0元");
//                }
                // TODO: 测试提现
                Intent withdraw = new Intent(this, WithdrawActivity.class);
                withdraw.putExtra("balance", d_balance);
                startActivity(withdraw);
                break;
            case R.id.tpwd_modify_txt:
                startAction(TPWDModifyActivity.class);
                break;
            case R.id.tpwd_forget_txt:
                startAction(TPWDForgetActivity.class);
                break;
        }
    }

    private void startAction(Class<?> cla) {
        if (cla != null) {
            startActivity(new Intent(this, cla));
        }
    }

    @Override
    public void onLoadDataSuccess(MyWalletResponse resp) {
        loadView.showDataView();
        if (resp != null) {
            myWallet = resp;
            d_balance = resp.getBalance();
            balanceTxt.setText("现金余额\n" + resp.getBalance());
            valBalanceTxt.setText("惠点\n" + resp.getValBalance());
            integralTxt.setText("积分\n" + resp.getIntegral());
            cardCountTxt.setText("已绑定" + resp.getCardCnt() + "个   ");
        } else {
            d_balance = 0d;
            balanceTxt.setText("现金余额\n0");
            valBalanceTxt.setText("惠点\n0");
            integralTxt.setText("积分\n0");
            cardCountTxt.setText("已绑定0个   ");
        }
    }

    @Override
    public void onLoadDataFailure() {
        loadView.showErrorView();
        balanceTxt.setText("现金余额\n0");
        valBalanceTxt.setText("惠点\n0");
        integralTxt.setText("积分\n0");
        cardCountTxt.setText("已绑定0个   ");
    }

    @Override
    public void onLoadAgain() {
        mPresenter.loadData();
    }
}
